package com.ww.idiom.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ww.core.activity.SlideMenuActivity;
import com.ww.core.util.CoreSharedPreferencesHelper;
import com.ww.idiom.R;
import com.ww.idiom.adapter.IdiomAdapter;
import com.ww.idiom.db.IdiomDbHelper;

/**
 * t图片浏览插件. 
 * @version 2013-7-11
 * @author wangwei
 */
public class MainActivity extends SlideMenuActivity {
	private ArrayList<String> list = new ArrayList<String>();
	private ViewPager pager;
	public static MainActivity instance = null;
	private TextView num, title;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_main, container, false);
		initView(view);
		return view;
	}

	protected void initView(View view) {
		findView(view);
		setTitle(view, "");
		getData();
		pager.setAdapter(new IdiomAdapter(context, list));
		pager.setOnPageChangeListener(new GuidePageChangeListener());
		if (list.size() > 0) {
			CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
			String str = h.getValue("index");
			if (str != null) {
				pager.setCurrentItem(Integer.parseInt(str));
				setTitleTxt(Integer.parseInt(str), list.get(Integer.parseInt(str)));
			} else {
				pager.setCurrentItem(0);
				setTitleTxt(0, list.get(0));
			}
		}

	}

	private void getData() {
		IdiomDbHelper s = new IdiomDbHelper(context);
		list = s.getWordList();
	}

	private void findView(View view) {
		num = (TextView) view.findViewById(R.id.num);
		pager = (ViewPager) view.findViewById(R.id.pager);
		title = (TextView) view.findViewById(R.id.ivTitleName);

		ImageButton right = (ImageButton) view.findViewById(R.id.right);
		right.setImageResource(R.drawable.icon_query);
		right.setVisibility(View.VISIBLE);
		right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent in = new Intent();
				in.setClass(context, QueryActivity.class);
				context.startActivity(in);
			}
		});
	}

	private final class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			setTitleTxt(arg0, list.get(arg0));
			int tt = 0;
			CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
			String str = h.getValue("index");
			if (str != null) {
				tt = Integer.parseInt(str);
			}
			if (arg0 > tt) {
				h.setValue("index", arg0 + "");
			}
		}
	}

	public void setTitleTxt(int index, String txt) {
		num.setText((index + 1) + "/" + list.size());
		title.setText(txt);
	}

}
